package com.libraryManage.commons.utils;



import com.libraryManage.commons.config.Const;

import javax.servlet.http.HttpServletRequest;

/**
 * SpringMVC工具类
 * @author shou
 *
 */
public class MVCUtil {
      
    /** 
     * 返回成功请求数据
     *  
     * @param obj 
     * @return 
     */  
    public static<T> ResponseEntity<T> response(T obj) {  
  
        ResponseEntity<T> re = new ResponseEntity<T>();
        
        re.setCode(Const.STAT_SUCC);
        re.setMsg(Const.MSG_SUCC);
        re.setData(obj);
        
        return re;
    } 
    
    /** 
     * 返回成功请求分页数据
     *
     * @param total
     * @param obj 
     * @return 
     */  
    public static<T> ResponseEntity<T> resPaging(int total,T obj) {  
  
        ResponseEntity<T> re = new ResponseEntity<T>();
        
        re.setCode(Const.STAT_SUCC);
        re.setMsg(Const.MSG_SUCC);
        re.setTotal(total);
        re.setData(obj);
        
        return re;
    }
    
    /**
     * 返回失败请求  
     * @param code
     * @param msg
     * @param obj
     * @return
     */
    public static<T> ResponseEntity<T> resFailure(int code, String msg, T obj) {
  
        ResponseEntity<T> re = new ResponseEntity<T>();
        
        re.setCode(code);
        re.setMsg(msg);
        re.setData(obj);
        
        return re;
    }
 
    /**
     * 无返回data请求  
     * @param code
     * @param msg
     * @return
     */
    public static SimpleResp response(int code, String msg){
        SimpleResp re = new SimpleResp();        
        re.setCode(code);
        re.setMsg(msg);        
        return re;
    }
    
    /**
     * 获取项目http访问根URL
     */
    public static String getAccessBaseUrl(HttpServletRequest req){
        
        return req.getScheme() + "://" + req.getServerName() + ":" + req.getServerPort() +
               req.getContextPath() + "/";
    }
    
    /**
     * 获取项目发布根路径
     */
    public static String getPublishPath(HttpServletRequest req){
        
        return req.getServletContext().getRealPath("/");
    }
}
